<?php


namespace backend\modules\tool\DataSource;


use backend\modules\tool\helpers\FileHelper;

class Script
{
    public $path;
    public $script;
    public function __construct($script)
    {
        $this->path=__DIR__."/Filesystem/script/";
        FileHelper::mkdir($this->path);
        if(!empty($script)){
            if(!(strrpos($script,"<?php")!==false)){
                $script="<?php ".PHP_EOL.$script;
            }
            $this->WriteScript($script);
            $this->LoadScript($script);
        }
    }
    public function LoadScript($script){
        $path=$this->path.md5($script).".php";
        require_once $path;
    }
    public function WriteScript($script){
        $path=$this->path.md5($script).".php";
        if(!is_file($path)){
            file_put_contents($path,$script);
        }
    }

}